package com.tarry.trade.counter.util;

import com.google.gson.Gson;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Type;

@UtilityClass
@Slf4j
public class GsonUtils {
    private static final Gson GSON = new Gson();

    public static String toJsonStr(Object object) {
        return GSON.toJson(object);
    }

    public static String toJsonStr(Object obj, Type type) {
        return GSON.toJson(obj, type);
    }

    public static <T> T fromJson(String json, Class<T> classOfT) {
        return GSON.fromJson(json, classOfT);
    }

    public static <T> T fromJson(String json, Type typeOfT) {
        return GSON.fromJson(json, typeOfT);
    }

    public static String toJson(Object object) {
        try {
            return GSON.toJson(object);
        } catch (Exception e) {
            log.error("序列化失败", e);
        }
        return StringUtils.EMPTY;
    }
}
